;; supercollider/ugen-compound.scm - (c) rohan drape, 2003-2004

;; The buffer at `bufnum' must have the same number of channels as
;; `inputs'.

(define (PingPong.ar bufnum inputs delayTime feedback rotate)
  (let* ((trig (Impulse.kr (Recip delayTime)))
	 (delayedSignals (PlayBuf.ar (length inputs) bufnum 1 trig 0 1))
	 (outputs (Add (Mul (rotate delayedSignals rotate) feedback)
		       inputs)))
    (RecordBuf.ar bufnum 0 1 0 1 1 trig outputs)
    outputs))

;; Delay aliases.

(define (DelayWr.ar bufnum in)
  (RecordBuf.ar in bufnum))

(define (Tap.ar bufnum delaytime)
  (PlayBuf.ar 1 bufnum 1 0 (Mul delaytime (Neg (SampleRate.ir))) 1))

;; Audio input.  Does not support MulAdd.

(defineK (AudioIn.ar (channel 1))
  (define (consecutive? l)
    (or (null? l)
	(null? (cdr l))
	(and (= (car l) (- (cadr l) 1))
	     (consecutive? (cdr l)))))
  (let ((offset (Sub (NumOutputBuses.ir) 1)))
    (if (not (list? channel))
	(In.ar (Add offset channel))
	(if (consecutive? channel)
	    (In.ar (Add offset (car channel)) (length channel))
	    (In.ar (Add offset channel))))))

(defineK (RunningSum.rms (in #f) (numsamp 40))
  (Sqrt (Mul (RunningSum.ar (Squared in) numsamp) (Recip numsamp))))

